package com.miniot.environment.utils;

import cn.hutool.core.map.MapUtil;
import com.miniot.business.utils.Oauth2Util;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Map;
import java.util.Optional;

public class BaseDbInfoUtils {
    private static final String TOKEN = "request$token";
    private static final String COMPANY = "request$company";




    private static void set(String key, Object value) {
        ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().setAttribute(key, value);
    }

    private static <T> T get(String key) {
        return (T) ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getAttribute(key);
    }

    private static Map getInfo() {
        Map jwtClaimsFromHeader = get(TOKEN);
        if (jwtClaimsFromHeader != null) {
            return jwtClaimsFromHeader;
        }
        jwtClaimsFromHeader = Oauth2Util.getJwtClaimsFromHeader(((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
        set(TOKEN, jwtClaimsFromHeader);
        return Optional.ofNullable(jwtClaimsFromHeader).orElse(MapUtil.of(new Object[]{
                new Object[]{"userid",1},
                new Object[]{"userName","saas"},
                new Object[]{"companyId",0},
                new Object[]{"companyName","saascompany"}
        }));
//        return jwtClaimsFromHeader;
    }

    /**
     * 获取当前操作人id
     *
     * @return
     */
    public static String getCurrentUserId() {
        return String.valueOf(getInfo().get("userid"));
    }

    /**
     * 获取当前操作人姓名
     *
     * @return
     */
    public static String getCurrentUserName() {
        return String.valueOf(getInfo().get("name"));
    }

    /**
     * 获取当前操作人所属的公司id
     *
     * @return
     */
    public static String getCurrentCompanyId() {
        return String.valueOf(getInfo().get("companyId"));
    }

    public static String getCurrentCompanyName() {
        return String.valueOf(getInfo().get("companyName"));
    }


    /**
     * 判断公司类别   0总公司 1子公司 2基地  4违法的信息
     *
     * @return
     */
    public static Integer companyLevel() {
        String level = (String) getInfo().get("level");
        if (level == null) {
            return 4;
        }
        Integer status = 4;
        switch (level) {
            case "companyAdmin":
                status = 0;
                break;
            case "companyAdminNormal":
                status = 0;
                break;
            case "companyUser":
                status = 1;
                break;
            case "companyUserNormal":
                status = 1;
                break;
            case "user":
                status = 2;
                break;
            case "userNormal":
                status = 2;
                break;
            case "saasAdmin":
                status = 3;
            default:
        }
        return status;
    }


}